package com.example.syyao.myapp.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.syyao.myapp.MainActivity;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Tool.Picture_tool;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.StackAdapter;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TestStackAdapter extends StackAdapter<String> {
    List<Map<String,Object>> myInformation;
    public TestStackAdapter(Context context,List<Map<String,Object>> temp) {

        super(context);
        myInformation = new ArrayList<>();
        this.myInformation = temp;
    }

    @Override
    public void bindView(String data, int position, CardStackView.ViewHolder holder) {
            ColorItemViewHolder h = (ColorItemViewHolder) holder;
            h.onBind(data, position);
    }

    @Override
    protected CardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) {
        View view;
        view = getLayoutInflater().inflate(R.layout.list_card_item_larger_header, parent, false);
        return new ColorItemViewHolder(view);

    }

    @Override
    public int getItemViewType(int position) {
//        if (position == 6) {//TODO TEST LARGER ITEM
//            return R.layout.list_card_item_larger_header;
//        } else if (position == 10) {
//            return R.layout.list_card_item_with_no_header;
//        }else {
            return R.layout.list_card_item_larger_header;
//        }
    }

    class ColorItemViewHolder extends CardStackView.ViewHolder {
        View mLayout;
        View mContainerContent;
        TextView mTextTitle;
        ImageView mImageView;
        TextView nickname;
        ImageView head_picture;
        TextView time;


        @SuppressLint("ResourceType")
        public ColorItemViewHolder(View view) {
            super(view);
            mLayout = view.findViewById(R.id.frame_list_card_item);
            mContainerContent = view.findViewById(R.id.container_list_content);
            mTextTitle = (TextView) view.findViewById(R.id.text_list_card_title);
            mImageView=view.findViewById(R.id.home_p_p);
            head_picture = view.findViewById(R.id.personal_head);
            time = view.findViewById(R.id.textView11);
            nickname = view.findViewById(R.id.textView10);
        }


        @Override
        public void onItemExpand(boolean b) {
            mContainerContent.setVisibility(b ? View.VISIBLE : View.GONE);
        }

        public void onBind(String data, int position) {
          //  mLayout.getBackground().setColorFilter(ContextCompat.getColor(getContext(), data), PorterDuff.Mode.SRC_IN);
            mTextTitle.setText(String.valueOf(position));
//            head_picture.setImageResource();
            System.out.println(myInformation.size());
            time.setText(myInformation.get(position).get("time").toString());
            nickname.setText(myInformation.get(position).get("nickname").toString());



            if(position == getItemCount()){
                System.out.println("data:"+data+"position:"+position);
                onItemExpand(true);
            }
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.login_pic)		;		//加载成功之前占位图

         //   System.out.println("http://120.79.205.71/picture/"+myInformation.get(position).get("mypicture").toString());
            Glide.with(getContext()).asBitmap().load("http://120.79.205.71/picture/"+myInformation.get(position).get("head_picture").toString()).apply(options).into(head_picture);
            Glide.with(getContext()).asBitmap().load("http://120.79.205.71/picture/"+myInformation.get(position).get("mypicture").toString()).apply(options).into(mImageView);
            //mImageView.setImageBitmap(data);

            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Picture_tool picture_tool = new Picture_tool();
                    picture_tool.init(getContext(),mImageView);
                    picture_tool.dialog.show();
                }
            });
        }

        public void onBindlast(Integer data,int position){
            if(position == getItemCount()){
              //  mLayout.getBackground().setColorFilter(ContextCompat.getColor(getContext(), data), PorterDuff.Mode.SRC_IN);
                mTextTitle.setText(String.valueOf(position));
                onItemExpand(true);
            }
        }
        @Override
        protected void onAnimationStateChange(int state, boolean willBeSelect) {
            super.onAnimationStateChange(state, willBeSelect);
            if (state == CardStackView.ANIMATION_STATE_START && willBeSelect) {
                onItemExpand(true);
            }
            if (state == CardStackView.ANIMATION_STATE_END && !willBeSelect ) {
                onItemExpand(false);
            }

        }
    }


}
